package com.xmall.product.service.impl.other;

import com.xmall.msg.queue.ProductModule;
import com.xmall.msg.base.MsgSourceType;
import com.xmall.msg.entity.es.ProductUpdateMsg;
import com.xmall.msg.entity.product.StockDeductMsg;
import com.xmall.msg.entity.product.StockLockCheckMsg;
import com.xmall.msg.entity.product.StockLockMsg;
import com.xmall.msg.entity.product.StockRevertMsg;
import com.xmall.product.define.constants.ProductProperties;
import com.xmall.product.service.other.IMsgService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class MsgServiceImpl implements IMsgService {

    @Resource
    private ProductProperties productProperties;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 锁定库存消息
     */
    @Override
    public void sendStockLockMsg(Long orderId) {
        StockLockMsg msg = new StockLockMsg().build(MsgSourceType.PRODUCT, ProductModule.StockLock.STOCK_LOCK_FANOUT_EXCHANGE, ProductModule.StockLock.STOCK_LOCK_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(ProductModule.StockLock.STOCK_LOCK_FANOUT_EXCHANGE, ProductModule.StockLock.STOCK_LOCK_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 锁定库存检查消息
     */
    @Override
    public void sendStockLockCheckMsg(Long orderId) {
        StockLockCheckMsg msg = new StockLockCheckMsg().build(MsgSourceType.PRODUCT, ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockLockCheck.DELAY_STOCK_LOCK_CHECK_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockLockCheck.DELAY_STOCK_LOCK_CHECK_ROUTING_KEY, msg, (message -> {
            message.getMessageProperties().setExpiration(productProperties.getStockLockCheckMilliseconds().toString());
            return message;
        }), new CorrelationData(msg.getCorrelationKey()));

    }

    /**
     * 库存恢复消息
     */
    @Override
    public void sendStockRevertMsg(Long orderId) {
        StockRevertMsg msg = new StockRevertMsg().build(MsgSourceType.PRODUCT, ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockRevert.STOCK_REVERT_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockRevert.STOCK_REVERT_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 库存扣减消息
     */
    @Override
    public void sendStockDeductMsg(Long orderId) {
        StockDeductMsg msg = new StockDeductMsg().build(MsgSourceType.PRODUCT, ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockDeduct.STOCK_DEDUCT_ROUTING_KEY).orderId(orderId);
        rabbitTemplate.convertAndSend(ProductModule.CommonExchange.STOCK_DIRECT_EXCHANGE, ProductModule.StockDeduct.STOCK_DEDUCT_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 商品更新消息
     */
    @Override
    public void sendProductUpdateMsg(Long spuId, Integer esVersion) {
        ProductUpdateMsg msg = new ProductUpdateMsg().build(MsgSourceType.PRODUCT, ProductModule.ProductUpdate.PRODUCT_UPDATE_FANOUT_EXCHANGE, ProductModule.ProductUpdate.PRODUCT_UPDATE_ROUTING_KEY).spuId(spuId).esVersion(esVersion);
        rabbitTemplate.convertAndSend(ProductModule.ProductUpdate.PRODUCT_UPDATE_FANOUT_EXCHANGE, ProductModule.ProductUpdate.PRODUCT_UPDATE_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));
    }

    /**
     * 商品删除消息
     */
    @Override
    public void sendProductDeleteMsg(Long spuId) {
        ProductUpdateMsg msg = new ProductUpdateMsg().build(MsgSourceType.PRODUCT, ProductModule.ProductDelete.PRODUCT_DELETE_FANOUT_EXCHANGE, ProductModule.ProductDelete.PRODUCT_DELETE_ROUTING_KEY).spuId(spuId);
        rabbitTemplate.convertAndSend(ProductModule.ProductDelete.PRODUCT_DELETE_FANOUT_EXCHANGE, ProductModule.ProductDelete.PRODUCT_DELETE_ROUTING_KEY, msg, new CorrelationData(msg.getCorrelationKey()));

    }

}
